package com.d3works.apps.project.web;

import com.d3works.apps.project.db.ResourceBooking;
import com.d3works.apps.project.exception.ProjectExceptionMessage;
import com.d3works.apps.project.service.ResourceBookingService;
import com.d3works.apps.project.service.app.AppProjectService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;

public class ResourceBookingController {

  public void checkIfResourceBooked(ActionRequest request, ActionResponse response) {
    if (Beans.get(AppProjectService.class).getAppProject().getCheckResourceAvailibility()) {
      ResourceBooking resourceBooking = request.getContext().asType(ResourceBooking.class);
      if (resourceBooking.getFromDate() != null
          && resourceBooking.getToDate() != null
          && Beans.get(ResourceBookingService.class).checkIfResourceBooked(resourceBooking)) {
        response.setError(I18n.get(ProjectExceptionMessage.RESOURCE_ALREADY_BOOKED_ERROR_MSG));
      }
    }
  }
}
